package com.tw.trips.service.helpers;

import java.util.Calendar;
import java.util.Date;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.neo4j.support.Neo4jTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.tw.trips.domain.Activity;
import com.tw.trips.domain.Alert;
import com.tw.trips.domain.Comment;
import com.tw.trips.domain.Location;
import com.tw.trips.domain.Traveller;
import com.tw.trips.domain.Trip;
import com.tw.trips.domain.Update;
import com.tw.trips.repository.LocationRepository;
import com.tw.trips.repository.TravellerRepository;
import com.tw.trips.service.LocationService;
import com.tw.trips.service.TravellerService;
import com.tw.trips.service.TripAlertService;

@Service
public class DatabasePopulator {

	@Autowired
	private LocationService locationService;
	@Autowired
	private TravellerService travellerService;
	@Autowired
	private LocationRepository locationRepository;
	@Autowired
	private TravellerRepository travellerRepository;
	@Autowired
	private Neo4jTemplate template;
	@Autowired
	private TripAlertService alertService;

	@Transactional
	public void populateDatabase() {
		travellerRepository.deleteAll();
		locationRepository.deleteAll();
		Traveller prasad = travellerService.register(new Traveller(true,
				"Prasad Krishnan", "prasad", "prasad_krishnan@mindtree.com"));
		Alert prasadAlert = alertService.getMockAlert();
		prasadAlert.setToBeAlerted(prasad);
		travellerService.addAlert(prasadAlert);
		
		Traveller sathish = travellerService.register(new Traveller(true,
				"Sathish Srinivasan", "sathish",
				"sathish_srinivasan@mindtree.com"));
		Alert satAlert = alertService.getMockAlert();
		satAlert.setToBeAlerted(sathish);
		travellerService.addAlert(satAlert);
		
		Traveller sripaul = travellerService.register(new Traveller(true,
				"Sripaul Asokan", "sripaul", "sripaul_asokan@mindtree.com"));
		Alert sriAlert = alertService.getMockAlert();
		sriAlert.setToBeAlerted(sripaul);
		travellerService.addAlert(sriAlert);
		
		Location chennai = locationRepository.save(new Location("Chennai"));
		Location bangalore = locationRepository.save(new Location("Bangalore"));

		Location bangaloreUpdate = locationService.putUpdateForLocation(
				bangalore, prasad, new Update("Hi Im in bangalore now ! ",
						"img url", new Date()));
		locationService.putCommentForUpdate(bangaloreUpdate.getUpdates()
				.iterator().next(), new Date(), sathish, "How are you ? ");

		Location chennaiUpdate = locationService.putUpdateForLocation(chennai,
				sripaul, new Update("In Chennai ", "img url", new Date()));
		locationService.putCommentForUpdate(chennaiUpdate.getUpdates()
				.iterator().next(), new Date(), prasad,
				"When are u coming back ? ");

		addTrips();
		addItHelps();

		System.out.println(template.fetch(prasad));
	}

	private void addItHelps() {
		Traveller prasad = travellerRepository
				.findByEmailID("prasad_krishnan@mindtree.com");
		Traveller sripaul = travellerRepository
				.findByEmailID("sripaul_asokan@mindtree.com");
		Traveller sathish = travellerRepository
				.findByEmailID("sathish_srinivasan@mindtree.com");
		for (Update update : sripaul.getUpdates()) {
			locationService.addItHepsToUpdate(update, sathish);
			for (Comment comment : update.getComments()) {
				locationService.addItHepsToComment(comment, sathish);
			}
		}
		for (Trip trip : prasad.getTrips()) {
			for (Activity activity : trip.getActivities()) {
				locationService.addItHelpsToActivity(activity, sripaul);
				locationService.addItHelpsToActivity(activity, sathish);
			}
		}
	}

	private void addTrips() {
		Traveller prasad = travellerRepository
				.findByEmailID("prasad_krishnan@mindtree.com");
		Traveller sathish = travellerRepository
				.findByEmailID("sathish_srinivasan@mindtree.com");
		Traveller sripaul = travellerRepository
				.findByEmailID("sripaul_asokan@mindtree.com");

		Location chennai = locationService.getLocation("Chennai");
		Location bangalore = locationService.getLocation("Bangalore");
		Location hyderabad = locationService.getOrAddLocation("Hyderabad");

		Set<Trip> prasadTrips = travellerService.addTrip(prasad, new Trip(
				chennai, bangalore, getDate(4, Calendar.FEBRUARY, 2012),
				getDate(12, Calendar.FEBRUARY, 2012), null, null, new Date()));
		travellerService
				.addTrip(
						sripaul,
						new Trip(hyderabad, chennai, getDate(24,
								Calendar.JANUARY, 2012), getDate(30,
								Calendar.JANUARY, 2012), null, null, new Date()));
		travellerService.addTrip(
				sathish,
				new Trip(hyderabad, chennai,
						getDate(25, Calendar.JANUARY, 2012), getDate(5,
								Calendar.FEBRUARY, 2012), null, null,
						new Date()));

		for (Trip trip : prasadTrips) {
			prasad = travellerService.addActivityToTrip(trip, new Activity(
					"Its rainin here in bangalore", "picasa", new Date(),
					prasad));
			for (Activity activity : prasad.getTrips().iterator().next()
					.getActivities()) {
				Traveller traveller = locationService.addItHelpsToActivity(
						activity, sathish);
				System.out.println("In trips " + traveller);
				System.out.println("Activities : "
						+ traveller.getTrips().iterator().next()
								.getActivities());
			}
		}
	}

	private Date getDate(int date, int month, int year) {
		Calendar calendar = Calendar.getInstance();
		calendar.clear();
		calendar.set(year, month, date);
		return calendar.getTime();
	}
}
